#if !defined(TrailHeader)
#define TrailHeader

#include <vector>

#include "node.h"
#include "reference.h"

// Record changes to variables, and perform backtracking if necessary
class Trail {
public:
  typedef unsigned int Pointer;

  class Record {
  public:
    Record () {
    }
    Record (Reference &r) :
      node ((Node*) &r) {
    }
    void undo () {
      static_cast<Reference&> (*node.node).unbound ();
    }
    pNode node; // Uses pNode to avoid reference node being deleted
  };

  void record (Reference &reference) {
    records.push_back (Record (reference));
  }

  Pointer state () {
    return records.size ();
  }

  void rollback () { // Undoes all
    rollback (0);
  }

  void rollback (Pointer point) {
    for(Records::iterator iter = records.begin () + point; iter
        < records.end (); iter++)
      (*iter).undo ();
    records.resize (point);
  }

  typedef std::vector<Record> Records;
  Records records;
};

#endif
